package com.dmxh.service.impl;

import com.dmxh.mapper.TbItemMapper;
import com.dmxh.pojo.TbItem;
import com.dmxh.service.ItemService;
import com.dmxh.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public Map<String, Object> queryItem() {
        List<ItemVO> collect = tbItemMapper.selectByExample(null)
                .stream().map(itemstram -> {
                    ItemVO itemVO = new ItemVO();
                    BeanUtils.copyProperties(itemstram, itemVO);
                    return itemVO;
                }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("itemlist", collect);
        return map;
    }


    @Override
    public TbItem queryItemBySeller(Long itemId) {
        TbItem tbItem = this.tbItemMapper.selectByPrimaryKey(itemId);
        return tbItem;
    }


    @Override
    public void updateItemNumById(List<TbItem> itemList) {
        for (TbItem tbItem1 : itemList) {
            TbItem tbItem = this.tbItemMapper.selectByPrimaryKey(tbItem1.getId());//根据id查询数据库
            tbItem.setStockCount(tbItem.getStockCount() - tbItem1.getNum());/*现有库存  -  数量*/
            this.tbItemMapper.updateByPrimaryKeySelective(tbItem);
        }
    }
}
